package com.example.springboot.controller;

import com.example.springboot.base.BaseResult;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by Intellij IDEA.
 * User:  GaoSong
 * Date:  2022/7/15
 */
@RestController
@RequestMapping("/usr")
public class UserController {
    @Autowired(required = false)
    private UserMapper userMapper;

    @RequestMapping("/select")
    public BaseResult findall(@RequestParam Integer id){
        List<User> all = userMapper.findAll(id);
        if (all!=null){
            return new BaseResult("200","Success!",all);
        }else {
            return new BaseResult("400","Failed!","");
        }
    }
    @RequestMapping("/insert")
    public BaseResult insertuser(@RequestBody Map map){
        int i = userMapper.insertUser(map);
        if (i!=0){
            return new BaseResult("200","Success!","");
        }else {
            return new BaseResult("400","Failed!","");
        }
    }
    @RequestMapping("/update")
    public BaseResult updateuser(@RequestBody Map map){
        int i = userMapper.updateUser(map);
        if (i!=0){
            return new BaseResult("200","Success!","");
        }else {
            return new BaseResult("400","Failed!","");
        }
    }
    @RequestMapping("/delete")
    public BaseResult deleteuser(@RequestParam Integer id){
        int i = userMapper.deleteUser(id);
        if (i!=0){
            return new BaseResult("200","Success!","");
        }else {
            return new BaseResult("400","Failed!","");
        }
    }
    @RequestMapping("/login")
    public BaseResult login(@RequestBody Map map){
        String name = (String) map.get("name");
        String password = (String) map.get("password");
        List<User> userLogin = userMapper.findUserLogin(name);
        String password1 = null;
        for (User user : userLogin) {
             password1 = user.getPassword();
        }
        if (password.equals(password1)){
            return new BaseResult("200","Success!","");
        }else {
            return new BaseResult("400","Failed!","");
        }
    }
}
